package woaini.wind.bot.code.impl.weChatFerry.internal;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import woaini.wind.bot.code.impl.weChatFerry.internal.dto.TextDTO;
import woaini.wind.bot.core.bot.Bot;
import woaini.wind.bot.core.event.action.ActionResponse;
import woaini.wind.bot.core.event.segment.Messages;
import woaini.wind.bot.core.event.segment.Segment;
import woaini.wind.bot.core.event.segment.impl.Text;
import woaini.wind.bot.core.internal.Internal;

import java.util.List;

public class WeChatFerryBotInternal extends Internal {

  private final String host;



  public WeChatFerryBotInternal(Bot bot) {
    super(bot);
    host = bot.getConfig().getHttpHost();
  }

  @Override
  public ActionResponse sendGroupMessage(String groupId, Messages messages) {
    //群消息
    sendText(groupId,messages);
    return null;
  }

  @Override
  public ActionResponse sendPrivateMessage(String userId, Messages messages) {

    sendText(userId,messages);
    return null;
  }

  public void sendText(String sendId, Messages messages) {
    //获取纯文本消息
    List<Segment> elements = messages.getElements();
    String message = null;
    for (Segment element : elements) {
      if (element instanceof Text){
        message = ((Text) element).getData().getText();
      }
    }

    TextDTO textDTO = new TextDTO();
    textDTO.setMsg(message);
    textDTO.setReceiver(sendId);
    textDTO.setAters("");

    String url = host + "/text";
    HttpRequest.post(url).body(JSONObject.toJSONString(textDTO)).execute().body();
  }
}
